package com.gaeainfo.modules.alarm.process.service;

import com.alibaba.fastjson.JSONObject;
import com.gaeainfo.constant.GaeaConstant;
import com.gaeainfo.framework.core.utils.common.GaeaCommonUtil;
import com.gaeainfo.modules.alarm.process.dao.AlarmProcessDao;
import com.gaeainfo.modules.alarm.process.dto.AlarmDto;
import com.gaeainfo.modules.alarm.process.dto.AlarmProcessDto;
import com.gaeainfo.modules.alarm.process.entity.AlarmEntity;
import com.gaeainfo.modules.alarm.process.entity.AlarmProcessEntity;
import com.gaeainfo.modules.base.adapter.CollectionAdapter;
import com.gaeainfo.modules.base.adapter.ProcessAdapter;
import com.gaeainfo.modules.mobile.dao.MobileDao;
import com.gaeainfo.modules.mobile.entity.MobileStatusEntity;
import com.gaeainfo.modules.mobile.service.MobileService;
import com.gaeainfo.websocket.WebSocketHandlerHub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketHandler;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Map;

/**
 * Created by Caven on 2018/7/10.
 */

@Service("alarmProcessService")
public class AlarmProcessServiceImpl implements AlarmProcessService {

    @Bean
    public WebSocketHandler webSocketHandlerHub() {
        return new WebSocketHandlerHub();
    }

    @Resource(name = "alarmProcessAdapter")
    private ProcessAdapter alarmProcessAdapter;

    @Autowired
    private AlarmProcessDao alarmProcessDao;

    @Autowired
    private MobileService mobileService;

    @Override
    public AlarmProcessDto getProcess(String processId) throws Exception {
        Map<String, Object> result = alarmProcessDao.findProcessDetailsById(processId);
        AlarmDto alarmDto = new AlarmDto();
        alarmDto.setAlarmId(GaeaCommonUtil.trimToEmpty(result.get("ALARM_ID")));
        alarmDto.setAlarmTime(GaeaCommonUtil.trimToEmpty(result.get("ALARM_TIME")));
        alarmDto.setAddress(GaeaCommonUtil.trimToEmpty(result.get("ADDRESS")));
        alarmDto.setAlarmPerson(GaeaCommonUtil.trimToEmpty(result.get("ALARM_PERSON")));
        alarmDto.setContent(GaeaCommonUtil.trimToEmpty(result.get("CONTENT")));
        alarmDto.setDeptName(GaeaCommonUtil.trimToEmpty(result.get("DEPT_NAME")));
        alarmDto.setTypeDesc(GaeaCommonUtil.trimToEmpty(result.get("TYPE_DESC")));
        AlarmProcessDto alarmProcessDto = new AlarmProcessDto();
        alarmProcessDto.setAlarmInfo(alarmDto);
        alarmProcessDto.setProcessId(GaeaCommonUtil.trimToEmpty(result.get("PROCESS_ID")));
        alarmProcessDto.setStatus(GaeaCommonUtil.trimToEmpty(result.get("STATUS")));
        alarmProcessDto.setStatusDesc(GaeaCommonUtil.trimToEmpty(result.get("STATUS_DESC")));
        alarmProcessDto.setDeptName(GaeaCommonUtil.trimToEmpty(result.get("DEPT_NAME")));
        alarmProcessDto.setPoliceNo(GaeaCommonUtil.trimToEmpty(result.get("POLICE_NO")));
        alarmProcessDto.setPoliceName(GaeaCommonUtil.trimToEmpty(result.get("POLICE_NAME")));
        return alarmProcessDto;
    }

    @Override
    public String updateProcess(String processId, String status, String lngLat, String policeNo, String policeName) throws Exception {
        AlarmProcessEntity alarmProcessEntity = alarmProcessDao.findAlarmProcessById(processId);
        if (alarmProcessEntity == null || status.equals(alarmProcessEntity.getStatus()) || "-1".equals(alarmProcessEntity.getStatus())) {
            return GaeaConstant.Result.FAIL;
        }
        Timestamp now = new Timestamp(System.currentTimeMillis());
        String mobileStatus = "";
        alarmProcessEntity.setStatus(status);
        switch (status) {
            case "1"://接收
                if (!GaeaCommonUtil.isNullOrEmpty(policeNo)) {
                    alarmProcessEntity.setPoliceNo(policeNo);
                }
                if (!GaeaCommonUtil.isNullOrEmpty(policeName)) {
                    alarmProcessEntity.setPoliceNo(policeName);
                }
                alarmProcessEntity.setReceiveTime(now);
                alarmProcessEntity.setReceiveLngLat(lngLat);
                mobileStatus = GaeaConstant.MobileStatus.ALARMING;
                break;
            case "2"://出发
                alarmProcessEntity.setDepartureTime(now);
                break;
            case "3"://到达
                alarmProcessEntity.setArriveTime(now);
                alarmProcessEntity.setArriveLngLat(lngLat);
                break;
            case "4"://完成
                alarmProcessEntity.setFinishTime(now);
                mobileStatus = GaeaConstant.MobileStatus.BACKUP;
                break;
            case "N"://无需出警
                alarmProcessEntity.setFinishTime(now);
                mobileStatus = GaeaConstant.MobileStatus.BACKUP;
                alarmProcessEntity.setRemark("无需出警");
                break;
            case "R"://退回
                alarmProcessEntity.setRemark("退回");
                mobileStatus = GaeaConstant.MobileStatus.BACKUP;
                break;
            default:
                break;
        }
        alarmProcessEntity.setUpdatedTime(now);
        alarmProcessDao.updateProcess(alarmProcessEntity);
        if (!GaeaCommonUtil.isNullOrEmpty(mobileStatus)) {
            mobileService.createMobileStatus(alarmProcessEntity.getDeptId(), alarmProcessEntity.getPoliceNo(), alarmProcessEntity.getPoliceName(), mobileStatus);
        }

        //推送消息
        JSONObject messJson = new JSONObject();
        messJson.put("type", GaeaConstant.MessType.STATUS);
        messJson.put("alarmId", alarmProcessEntity.getAlarmId());
        messJson.put("status", status);
        ((WebSocketHandlerHub) webSocketHandlerHub()).sendMessageToUsers(messJson.toJSONString());

        //同步到DS
        alarmProcessAdapter.pushMessToDs("");

        return GaeaConstant.Result.SUCCESS;


    }

    @Override
    public String updateAlarmLocation(String alarmId, double lng, double lat, String deptId) throws Exception {
        AlarmEntity alarmEntity = alarmProcessDao.findAlarmById(alarmId);
        if (alarmEntity == null) {
            return GaeaConstant.Result.FAIL;
        }
        alarmEntity.setLng(lng);
        alarmEntity.setLat(lat);
        alarmProcessDao.updateAlarm(alarmEntity);
        //推送消息
        JSONObject messJson = new JSONObject();
        messJson.put("type", GaeaConstant.MessType.LOCATION);
        messJson.put("alarmId", alarmId);
        messJson.put("lng", lng);
        messJson.put("lat", lat);
        ((WebSocketHandlerHub) webSocketHandlerHub()).sendMessageToUsers(messJson.toJSONString());
        return GaeaConstant.Result.SUCCESS;
    }


}
